package io.xxx.xbutler.common;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RIdGenerator;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class IdWorker {

    private final Map<IdType, RIdGenerator> map = new HashMap<>();

    public IdWorker(RedissonClient client) {
        for (IdType idType : IdType.values()) {
            RIdGenerator idGenerator = client.getIdGenerator(idType.getKey());
            idGenerator.tryInit(idType.getInitValue(), 100);
            this.map.put(idType, idGenerator);
        }
    }

    public Long nextId(IdType idType) {
        return map.get(idType).nextId();
    }
}
